import express from 'express'
import bodyParser from 'body-parser'
import fs from 'fs'
import path from 'path'

const app = express();

const port = 3000;

app.use(bodyParser.json());


import {
    res_pools,
    order_spu,
    project_version,
    hosts,
    placeNewPurchaseOrder,
    placeExpansionOrContractionOrder,
    placeUnsubscribeOrRecoverOrDestroyOrder,
    queryOrderPrice,
    userInfo,
    ticket_res,
} from './mock/index.mjs'

// 分页函数
const getPageData = (pageIndex = 1, pageSize = 10, list_data) => {
    const startIndex = (pageIndex - 1) * pageSize
    const endIndex = pageIndex * pageSize

    const data = list_data.slice(startIndex, endIndex)

    return {
        total: list_data.length,
        pages: pageIndex,
        size: Math.ceil(list_data.length / pageSize),
        list: data
    }
}

// 资源池列表api
app.get('/api/res_pools', (req, res) => {
    res.json(res_pools)
})

// 根据 id 查询
// /paas_service/admin/v1/order/resPools/{组件编码}
app.get('/paas_service/admin/v1/order/resPools/:id', (req, res) => {
    const { id } = req.params
    console.log('id: ' + id)
    const target = res_pools.data.find(item => item.resPoolId === Number(id))
    res.send(target || {})
})

// 产品规格查询
// pass_service/admin/v1/order/spu
app.get('pass_service/admin/v1/order/spu', (req, res) => {
    const { spuCode, resPoolCode } = req.query
    console.log(spuCode)
    console.log(resPoolCode)
    res.json(order_spu)
})

// 组件版本查询 (资源池预置区)
// /openapi/v1/versions/projectVersions/{projectCode}
app.get('/openapi/v1/versions/projectVersions/:projectCode', (req, res) => {
    const { projectCode } = req.params
    project_version.projectCode = projectCode
    res.send(project_version)
})

// 组件介质查询接口 (资源池预置区) TODO 有不确定返回格式
// /openapi/v1/versions/projectVersion
app.get('/openapi/v1/versions/projectVersion', (req, res) => {
    // 入参
    const { projectCode, projectVersionNo, cpuType, osType, deploy_mode } = req.query
})

// 组件介质下载接口 TODO 最后再弄
app.get('/download', (req, res) => {
    const {} = req.query
    console.log('req: ' + req)
    const filePath = path.join('./mock/index.mjs')
    const fileContent = fs.readFileSync(filePath)
    res.setHeader('Content-Type', 'application/octet-stream');
    res.setHeader('Content-Disposition', `attachment;filename=${path.basename(filePath)}`);
    res.end(fileContent);
})

// 查询租户自管主机列表
// /paas_service/ctcmp/v1/hosts
app.get('/paas_service/ctcmp/v1/hosts', (req, res) => {
    const { resPoolId, pageSize, pageNow, keyword, vpcCode, subNetCode, cpuType, osType, machine } = req.query

    console.log('resPoolId: ' + resPoolId)
    console.log('pageSize: ' + pageSize)
    console.log('pageNow: ' + pageNow)
    console.log('keyword: ' + keyword)
    console.log('vpcCode: ' + vpcCode)
    console.log('subNetCode: ' + subNetCode)
    console.log('cpuType: ' + cpuType)
    console.log('osType: ' + osType)
    console.log('machine: ' + machine)

    const result = {
        'code': 0,
        'data': getPageData(pageNow, pageSize, hosts.data)
    }
    res.json(result)
})

// /paas_service/order/v1/placeNewPurchaseOrder
// 产品订购下单
app.post('/paas_service/order/v1/placeNewPurchaseOrder', (req, res) => {
    const request = req.body

    console.log('reqBody: ', request)

    placeNewPurchaseOrder.sourceSeq = request.sourceSeq

    const result = {
        'code': 0,
        'msg': 'success',
        'data': placeNewPurchaseOrder
    }
    res.json(result);
})


// /paas_service/order/v1/placeExpansionOrder
// 产品扩容下单
app.post('/paas_service/order/v1/placeExpansionOrder', (req, res) => {
    const request = req.body

    console.log('reqBody: ', request)

    placeExpansionOrContractionOrder.sourceSeq = request.sourceSeq

    const result = {
        'code': 0,
        'msg': 'success',
        'data': placeExpansionOrContractionOrder
    }
    res.json(result);
})

// /paas_service/order/v1/placeContractionOrder
// 产品缩容下单
app.post('/paas_service/order/v1/placeContractionOrder', (req, res) => {
    const request = req.body

    console.log('reqBody: ', request)

    placeExpansionOrContractionOrder.sourceSeq = request.sourceSeq

    const result = {
        'code': 0,
        'msg': 'success',
        'data': placeExpansionOrContractionOrder
    }
    res.json(result);
})

// /paas_service/order/v1/placeUnsubscribeOrder
// 产品退订下单
app.post('/paas_service/order/v1/placeUnsubscribeOrder', (req, res) => {
    const request = req.body

    console.log('reqBody: ', request)

    placeUnsubscribeOrRecoverOrDestroyOrder.sourceSeq = request.sourceSeq

    const result = {
        'code': 0,
        'msg': 'success',
        'data': placeUnsubscribeOrRecoverOrDestroyOrder
    }
    res.json(result);
})

// /paas_service/order/v1/placeRecoverOrder
// 产品恢复下单
app.post('/paas_service/order/v1/placeRecoverOrder', (req, res) => {
    const request = req.body

    console.log('reqBody: ', request)

    placeUnsubscribeOrRecoverOrDestroyOrder.sourceSeq = request.sourceSeq

    const result = {
        'code': 0,
        'msg': 'success',
        'data': placeUnsubscribeOrRecoverOrDestroyOrder
    }
    res.json(result);
})

// /paas_service/order/v1/placeDestroyOrder
// 产品注销下单
app.post('/paas_service/order/v1/placeDestroyOrder', (req, res) => {
    const request = req.body

    console.log('reqBody: ', request)

    placeUnsubscribeOrRecoverOrDestroyOrder.sourceSeq = request.sourceSeq

    const result = {
        'code': 0,
        'msg': 'success',
        'data': placeUnsubscribeOrRecoverOrDestroyOrder
    }
    res.json(result);
})

// /openapi/v1/order/queryOrderPrice
// 产品订单询价
app.post('/openapi/v1/order/queryOrderPrice', (req, res) => {
    const request = req.body

    console.log('reqBody: ', request)

    const result = {
        'code': 0,
        'msg': 'success',
        'data': queryOrderPrice
    }
    res.json(result);
})

// 用户查询
// /paas_service/iam/v2/users/{userName}/userinfo
app.get('/paas_service/iam/v2/users/:userName/userinfo', (req, res) => {
    const { userName } = req.params

    console.log('userName: ', userName)

    res.json(userInfo)
})

// 单点登录
// /auth/login
// http://10.142.232.166:9002/auth/login?service=url_encode(callback_url)
app.get('/auth/login', (req, res) => {
    const { service } = req.query

    console.log('service: ', service)

    res.json(service)
})

// 校验ticket
// /auth/p3/serviceValidate
app.get('/auth/p3/serviceValidate', (req, res) => {
    const { service, ticket } = req.query

    console.log('service: ', service)
    console.log('ticket: ', ticket)

    res.json(ticket_res)
})


// 工单下发/工单回单
app.post('/api/send|receipt', (req, res) => {
    const reqBody = req.body

    console.log('reqBody: ' + reqBody)

    const result = {
        'code': 0,
        'msg': 'success',
        'data': {}
    }

    res.json(result)
})

app.listen(port, () => {
    console.log(`Server started at http://localhost:${port}`);
});